﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;

namespace h1
{
    /*
     * h1. 做一个窗体应用程序，接收用户输入的关键字，用户点击搜索时使用baidu搜索
     * 引擎搜索关键字，从搜索结果中，摘抄文字部分的前200个字，分别显示到两个多行
     * 文本框内。请使用多线程方式、并行编程或者异步编程方式完成任务。
     */
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string result1 = null;
            string result2 = null;
            if(textBox1.Text == "")
            {
                textBox1.Text = "请输入关键字";
                return;
            }
            BaiduCrawler crawler = new BaiduCrawler();
            crawler.Word = textBox1.Text;
            Thread thread1 = new Thread(new ThreadStart(() =>
            {
                result1 = crawler.Crawl();
            }));
            thread1.Start();
            thread1.Join();
            richTextBox1.Text = result1;
            Thread thread2 = new Thread(new ThreadStart(() =>
            {
                result2 = crawler.Crawl();
            }));
            thread2.Start();
            thread2.Join();
            richTextBox2.Text = result2;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string result1 = null;
            string result2 = null;
            if (textBox1.Text == "")
            {
                textBox1.Text = "请输入关键字";
                return;
            }
            BaiduCrawler crawler = new BaiduCrawler();
            crawler.Word = textBox1.Text;
            Parallel.Invoke(new Action[]
            {
                () =>
                {
                    result1=crawler.Crawl();
                },
                ()=>
                {
                    result2=crawler.Crawl();
                }
            });
            richTextBox1.Text= result1;
            richTextBox2.Text= result2;
        }
    }
}
